#!/bin/bash
#
# Python 2 编译器脚本（pypy）

DEST="$1" ; shift
MAINSOURCE="${ENTRY_POINT:-$1}"

# 当未开启 set -e 时，通过使用该函数来保证命令运行正确
expectsuccess ()
{
    set +e
    exitcode=0
    "$@"
    exitcode=$?
    [ "$exitcode" -ne 0 ] && exit $exitcode
    set -e
}

# 检查 python 代码的语法，以便抛出 CE
expectsuccess python2 -m py_compile "$@"
rm -f -- *.pyc

if [ ! -r "$MAINSOURCE" ]; then
    echo "Entry point '$MAINSOURCE' is not readable, falling to '$1'"
    MAINSOURCE="$1"
fi

cat > "$DEST" <<EOF
#!/bin/sh

if [ "\${0%/*}" != "\$0" ]; then
	cd "\${0%/*}"
fi

export HOME=/what/the/fuck

# 这里不编译 python 代码
exec pypy "$MAINSOURCE" "\$@"
EOF

chmod a+x "$DEST"
exit 0